package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;

import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.ICaptureActivityContract;

import cn.bingoogolapple.qrcode.core.BarcodeType;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

import static android.content.Context.VIBRATOR_SERVICE;

/**
 * Created by Administrator on 2018/8/10.
 */

public class CapturePersenter implements ICaptureActivityContract.ICaptureActivityPersenter{
    private ICaptureActivityContract.ICaptureActivityView iCaptureActivityView;
    private Context mContext;
    private ZXingView mZXingView;

    public CapturePersenter(ICaptureActivityContract.ICaptureActivityView iCaptureActivityView) {
        this.iCaptureActivityView = iCaptureActivityView;
    }

    @Override
    public void initView(ZXingView zXingView,Context context) {
        mZXingView=zXingView;
        mContext=context;
    }

    @Override
    public void OnClickedBack() {
        Intent intent=new Intent();
        intent.putExtra("result","");
        iCaptureActivityView.toSetResult(2,intent);

    }

    @Override
    public void onStart() {
        // 切换成扫描二维码样式
        mZXingView.changeToScanQRCodeStyle();
        // 只识别 QR_CODE
        mZXingView.setType(BarcodeType.ONLY_QR_CODE, null);
        // 打开后置摄像头开始预览，但是并未开始识别
        mZXingView.startCamera();
        // 仅识别扫描框中的码
        mZXingView.getScanBoxView().setOnlyDecodeScanBoxArea(true);
        // 开始识别
        mZXingView.startSpot();

    }

    @Override
    public void onStop() {
        // 停止识别
        mZXingView.stopSpot();
        // 关闭摄像头预览，并且隐藏扫描框
        mZXingView.stopCamera();
    }

    @Override
    public void onDestroy() {
        mZXingView.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        iCaptureActivityView.vibrate();
        Intent intent=new Intent();
        intent.putExtra("result",result);
        iCaptureActivityView.toSetResult(1,intent);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Intent intent=new Intent();
        intent.putExtra("result","");
        iCaptureActivityView.toSetResult(2,intent);

    }
}
